package com.cntv.viewmodeldemo

import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.cntv.androidviewmmodel.AndroidVMActivity
import com.cntv.viewmodeldemo.databinding.MainActivity
import com.cntv.viewmodeldemo.fragment.UsersActivity
import com.cntv.viewmodeldemo.livedata.ViewModelLvieDataActivity
import com.cntv.viewmodeldemo.onlyviewmodel.ViewModelActivity
import com.cntv.viewmodeldemo.savestate.KTXActivity
import com.cntv.viewmodeldemo.savestate.SaveStateViewModelActivity
import kotlinx.android.synthetic.main.activity_home.*

class HomeActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        button3.setOnClickListener { startAct(ViewModelActivity::class.java) }
        button4.setOnClickListener { startAct(ViewModelLvieDataActivity::class.java) }
        button5.setOnClickListener { startAct(MainActivity::class.java) }
        button6.setOnClickListener { startAct<KTXActivity>() }
        button7.setOnClickListener { startAct<SaveStateViewModelActivity>() }
        button8.setOnClickListener { startAct<AndroidVMActivity>() }
        button9.setOnClickListener { startAct<UsersActivity>() }
    }
    /**
     * 内联函数
     * 对Context 扩展方式启动Activity
     * reified 修饰内联函数的泛型 编译时会实化
     * */
    inline fun <reified T : Activity> Context.startAct() {
        startActivity(Intent(this, T::class.java))
    }

    /**
     * 启动Activity
     * @param clazz Class
     */
    private fun <T : Activity> startAct(clazz: Class<T>){
        startActivity(Intent(this,clazz))
    }
}